﻿using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;

namespace YmtAuth.Domain.Shard
{
    public static class YmtAuthSafeConstant
    {
        /// <summary>
        /// 请求接口ID
        /// </summary>
        public const string ApiId = "ApiId";

        /// <summary>
        /// 请求接口 - 登录
        /// </summary>
        public const string Api_login = "login";

        /// <summary>
        /// 请求接口 - IP区域认证
        /// </summary>
        public const string Api_ipareaauth = "ipareaauth";

        /// <summary>
        /// 请求接口 - 设备认证
        /// </summary>
        public const string Api_deviceauth = "deviceauth";

        public static int GetApiId(string apiName)
        {
            int apiId = 0;
            switch (apiName)
            {
                case Api_login: apiId = 1; break;
                case Api_ipareaauth: apiId = 2; break;
                case Api_deviceauth: apiId = 3; break;
            }

            return apiId;
        }

        /// <summary>
        /// 设备认证，合法的应用来源
        /// </summary>
        public static List<string> ValidSourceForAuthDevice { get; set; }

        /// <summary>
        /// 查询应用来源是否合法（FOR 设备认证）
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool IsValidSourceForAuthDevice(string source)
        {
            bool retVal = false;

            if (!string.IsNullOrEmpty(source))
                retVal = ValidSourceForAuthDevice.Any(r => string.Equals(source.ToLower(), r, System.StringComparison.InvariantCultureIgnoreCase));

            return retVal;
        }

        static YmtAuthSafeConstant()
        {
            var validAPPSourceForAuthDevice = (ConfigurationManager.AppSettings["ValidAPPSourceForAuthDevice"] ?? "seller.app.ymatou.com,app").Split(new char[] { ',' });

            ValidSourceForAuthDevice = validAPPSourceForAuthDevice.ToList();
        }
    }
}
